﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace MobileShop
{
    public partial class UserInfo : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SessionUser == null || SessionUser.RowCount == 0)
                this.Response.Redirect("Login.aspx?url=MyAccount.aspx");

            if (!IsPostBack)
            {
                PageMap = string.Format("My Account[{0}]|General Info[{1}]", this.ResolveUrl("~/MyAccount.aspx"), this.ResolveUrl("~/UserInfo.aspx"));

                txtEmail.Text = View_Customers.GetCurrentUser().s_Email;
                txtAddress.Text = View_Customers.GetCurrentUser().s_Address;
                txtPhone.Text = View_Customers.GetCurrentUser().s_Phone;
                txtName.Text = View_Customers.GetCurrentUser().s_CustomerName;

                //gridOrder.Rebind();
            }
        }

        protected void btnUpdate_OnClick(object sender, EventArgs e)
        {
            try
            {
                lblError.Text = "";
                lblNote.Text = "";
                if (txtEmail.Text != "" && Customers.GetCustomerByEmail(txtEmail.Text, View_Customers.GetCurrentUser().ID).RowCount > 0)
                {
                    lblError.Text = "This email has been used. Please provide another.";
                    return;
                }

                Customers cust = new Customers();
                cust.LoadByPrimaryKey(View_Customers.GetCurrentUser().ID);
                cust.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                cust.CustomerName = txtName.Text;
                cust.Address = txtAddress.Text;
                cust.Phone = txtPhone.Text;
                cust.Email = txtEmail.Text;

                cust.Save();

                lblNote.Text = "Update successfully!";
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("EditCustomerCtrl.ascx.cs", "lnkRegiester_OnClick", ex.Message + ex.StackTrace);
            }
        }
    }
}